import {NavigationActions, StackActions} from 'react-navigation';

const resetAction = StackActions.reset({
  index: 0,
  actions: [NavigationActions.navigate({routeName: 'login'})],
});
const baseUrl = 'http://121.36.28.99:8018/api/';
const cwGet = (url, params) => {
  return new Promise((resolve, reject) => {
    storage
      .load({key: 'token'})
      .then(token => {
        // console.log(baseUrl + url);
        fetch(baseUrl + url, {
          method: 'GET',
          headers: {
            'Content-Type': 'application/json',
            'api-access-token': token,
            'Accept-Encoding': 'gzip,deflate',
          },
        })
          .then(resphonse => {
            console.log('resphonse', resphonse);
            return resphonse.json();
          })
          .then(res => {
            console.log('res', res);
            // console.warn('cwGet',baseUrl + url,JSON.stringify(params),JSON.stringify(res));
            if (res.isSuccess) {
              resolve(res.data);
              return;
            } else {
              if (!res.isSuccess && res.errorMessage.includes('重新登录')) {
                alert(res.errorMessage || '接口请求失败');
                storage
                  .remove({
                    key: 'token',
                  })
                  .then(res => {
                    global.navigation.dispatch(resetAction);
                  });
                return;
              }
              reject(res.errorMessage);
              alert(res.errorMessage || '接口请求失败');
            }
          })
          .catch(err => {
            console.log(err);
            console.warn('cwGet网络异常',JSON.stringify(err));
            alert('网络异常');
            reject();
          });
      })
      .catch(err => {
        console.log('没有');
        console.warn('cwGet没有',JSON.stringify(err));
      });
  });
};

const cwPost = (url, params) => {
  return new Promise((resolve, reject) => {
    fetch(baseUrl + url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Accept-Encoding': 'gzip,deflate',
      },
      body: JSON.stringify(params),
    })
      .then(resphonse => resphonse.json())
      .then(res => {
        // console.warn('cwPost',baseUrl + url,JSON.stringify(params),JSON.stringify(res));
        if (res.isSuccess) {
          resolve(res.data);
        } else {
          if (!res.isSuccess || res.errorMessage.includes('重新登录')) {
            alert(res.errorMessage || '接口请求失败');
            storage
              .remove({
                key: 'token',
              })
              .then(res => {
                global.navigation.dispatch(resetAction);
              });
            return;
          }
          reject(res.errorMessage);
          alert(res.errorMessage || '接口请求失败');
        }
      })
      .catch(err => {
          console.warn('cwPost',JSON.stringify(err))
        reject(err);
      });
  });
};

export {cwGet, cwPost};
